package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.RoleService;
import com.bdqn.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserController
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, String usrName, @RequestParam(required = false,defaultValue = "0") Long roleId, @RequestParam(required = false,defaultValue = "1") int pageIndex) {
        List<Role> roles = roleService.list();
        Page page = new Page();
        page.setCurrent(pageIndex);
        User param = new User();
        param.setUsrName(usrName);
        param.setUsrRoleId(roleId);
        IPage userPage = userService.getPage(page, param);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        model.addAttribute("roles", roles);
        model.addAttribute("userPager", userPage);
        return "user/list";
    }
}
